<template>
  <div class="problem-description-tab">
    <div class="problem-content">
      <div class="content-section">
        <h3>题目描述</h3>
        <div class="description-text" v-html="questionData?.content"></div>
      </div>
      
      <div class="content-section" v-if="judgeCase && judgeCase.length > 0">
        <h3>示例</h3>
        <div class="examples">
          <div 
            v-for="(example, index) in judgeCase" 
            :key="index" 
            class="example-item"
          >
            <div class="example-header">示例 {{ index + 1 }}</div>
            <div class="example-content">
              <div class="input-output">
                <div class="input-section">
                  <span class="label">输入：</span>
                  <code class="code-text">{{ example.input }}</code>
                </div>
                <div class="output-section">
                  <span class="label">输出：</span>
                  <code class="code-text">{{ example.output }}</code>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="content-section" v-if="judgeConfig">
        <h3>限制条件</h3>
        <div class="constraints">
          <div class="constraint-item" v-if="judgeConfig.timeLimit">
            <span class="constraint-label">时间限制：</span>
            <span class="constraint-value">{{ judgeConfig.timeLimit }}ms</span>
          </div>
          <div class="constraint-item" v-if="judgeConfig.memoryLimit">
            <span class="constraint-label">内存限制：</span>
            <span class="constraint-value">{{ judgeConfig.memoryLimit }}MB</span>
          </div>
          <div class="constraint-item" v-if="judgeConfig.stackLimit">
            <span class="constraint-label">栈限制：</span>
            <span class="constraint-value">{{ judgeConfig.stackLimit }}MB</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface Props {
  questionData?: API.Question
}

const props = defineProps<Props>()

// 解析判题用例
const judgeCase = computed(() => {
  if (!props.questionData?.judgeCase) return []
  try {
    return JSON.parse(props.questionData.judgeCase)
  } catch (e) {
    console.error('解析判题用例失败:', e)
    return []
  }
})

// 解析判题配置
const judgeConfig = computed(() => {
  if (!props.questionData?.judgeConfig) return null
  try {
    return JSON.parse(props.questionData.judgeConfig)
  } catch (e) {
    console.error('解析判题配置失败:', e)
    return null
  }
})
</script>

<style scoped>
.problem-description-tab {
  height: 100%;
  overflow-y: auto;
}

.problem-content {
  padding: 20px 24px;
}

.content-section {
  margin-bottom: 32px;
}

.content-section:last-child {
  margin-bottom: 0;
}

.content-section h3 {
  font-size: 18px;
  font-weight: 600;
  color: #1f1f1f;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid #1890ff;
  display: inline-block;
}

.description-text {
  line-height: 1.8;
  color: #333;
  font-size: 15px;
}

.description-text :deep(p) {
  margin-bottom: 12px;
}

.description-text :deep(ul),
.description-text :deep(ol) {
  padding-left: 24px;
  margin: 12px 0;
}

.description-text :deep(li) {
  margin: 6px 0;
}

.examples {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.example-item {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  border-left: 4px solid #1890ff;
}

.example-header {
  font-weight: 600;
  color: #1f1f1f;
  margin-bottom: 12px;
  font-size: 14px;
}

.input-output {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-section,
.output-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

.label {
  font-weight: 500;
  color: #666;
  min-width: 40px;
}

.code-text {
  background: #fff;
  padding: 4px 8px;
  border-radius: 4px;
  font-family: 'Monaco', 'Menlo', monospace;
  font-size: 13px;
  color: #d63384;
  border: 1px solid #e9ecef;
}

.constraints {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.constraint-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: #f8f9fa;
  border-radius: 6px;
  border-left: 3px solid #52c41a;
}

.constraint-label {
  font-weight: 500;
  color: #666;
  min-width: 80px;
}

.constraint-value {
  color: #1f1f1f;
  font-weight: 600;
}

/* 自定义滚动条 */
.problem-description-tab::-webkit-scrollbar {
  width: 6px;
}

.problem-description-tab::-webkit-scrollbar-track {
  background-color: #f1f1f1;
  border-radius: 3px;
}

.problem-description-tab::-webkit-scrollbar-thumb {
  background-color: #c1c1c1;
  border-radius: 3px;
}

.problem-description-tab::-webkit-scrollbar-thumb:hover {
  background-color: #a8a8a8;
}
</style>
